﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using FinWeb3.Models.ModelView;
using System.Xml.Linq;
using System.Web.Routing;
using System.IO;
using System.Web.Mvc;

namespace FinWeb3.Models.Helpers
{
    public class HtmlHelpers
    {
        public static string MessageBox(string message)
        {
            if (string.IsNullOrEmpty(message))
                return string.Empty;

            return string.Format("<div class='ui-state-highlight ui-corner-all messageBox'><p><span class='ui-icon ui-icon-info' style='float: left; margin-right: .3em;'></span> {0}</div>", message);
        }

        public static string FormatedValue(decimal value)
        {
            if (value >= 0)
            {
                return string.Format("<span class='positiveValue'>{0:N2}</span>", value);
            }
            else
            {
                return string.Format("<span class='negativeValue'>{0:N2}</span>", value);
            }
        }

        public static string GetLink(Bill bill, BasicModelView view)
        {
            return bill.Description;
        }

        public static string GetMenu(RouteData data)
        {
            var html = new StringBuilder();

            XDocument siteMap = XDocument.Load(Path.Combine(HttpRuntime.AppDomainAppPath, "Web.sitemap"));

            var menuNodes = siteMap.Elements().First().Elements().First().Elements();

            foreach (var node in menuNodes)
            {
                html.Append(
                    string.Format(" :: <a href='{0}/{1}/{2}'>{3}</a>",
                        node.Attribute("url").Value,
                        data.Values["fromdate"],
                        data.Values["todate"],
                        node.Attribute("title").Value));
            }

            return html.ToString();
        }

        public static string GetSubMenu(RouteData data)
        {
            //TRY THIS!!!
            // page could be ViewContext.RouteData.Values["controller"]
            var path = "/" + ((Route)data.Route).Url;

            if(path.IndexOf("/{") > 0)
                path = path.Substring(0, path.IndexOf("/{"));



            var html = new StringBuilder();

            XDocument siteMap = XDocument.Load(Path.Combine(HttpRuntime.AppDomainAppPath, "Web.sitemap"));

            var menuNodes =
                siteMap.Elements().First()
                .Elements().First()
                .Elements().Where(x => x.Attribute("url").Value == path)
                .Elements();

            if (menuNodes.Count() == 0)
                return string.Empty;

            html.AppendLine("<div class='actionMenu'><b>Ações</b>");

            foreach (var node in menuNodes)
            {
                html.Append(
                    string.Format(" :: <a href='{0}/{1}/{2}'>{3}</a>",
                        node.Attribute("url").Value,
                        data.Values["fromdate"],
                        data.Values["todate"],
                        node.Attribute("title").Value));
            }

            html.AppendLine("</div>");

            return html.ToString();
        }

        public static string GetLineActionMenu(Bill bill, RouteData data)
        {
            var html = new StringBuilder();
            var payment = new StringBuilder();

            html.Append("<div class='lineActionMenu'>");

            if(bill.HasDetailList)
                html.Append(string.Format("<a href='/Home/AddDetail/{1}/{2}/{3}'>Linhas</a> :: ", data.Values["controller"], data.Values["fromdate"], data.Values["todate"], bill.Id));

            if (bill.GroupId > 0)
                html.Append(string.Format("<a href='/{0}/EditGroup/{1}/{2}/{3}'>Editar grupo</a> :: ", data.Values["controller"], data.Values["fromdate"], data.Values["todate"], bill.GroupId));

            html.Append(string.Format("<a href='/{0}/EditBill/{1}/{2}/{3}'>Editar</a> :: ", data.Values["controller"], data.Values["fromdate"], data.Values["todate"], bill.Id));

            if (data.Values["controller"] == "CashFlow")
                html.Append(string.Format("<a href='/{0}/PayBill/{1}/{2}/{3}'>Pagar</a> :: ", data.Values["controller"], data.Values["fromdate"], data.Values["todate"], bill.Id));


            html.Append(string.Format("<a href='/{0}/DeleteBill/{1}/{2}/{3}'>Excluir</a>", data.Values["controller"], data.Values["fromdate"], data.Values["todate"], bill.Id));

            html.Append("</div>");

            return html.ToString();
        }

        public static IEnumerable<SelectListItem> WalletItems()
        {
            var walletItems = new List<SelectListItem>();
            walletItems.Add(new SelectListItem()
               {
                   Text = "Fora da carteira",
                   Value = ((int)WalletBillStatus.NotInWallet).ToString(),
                   Selected = true
               });
            walletItems.Add(new SelectListItem()
            {
                Text = "Entrada",
                Value = ((int)WalletBillStatus.InWallet).ToString(),
                Selected = false
            });
            walletItems.Add(new SelectListItem()
            {
                Text = "Saída",
                Value = ((int)WalletBillStatus.OutWallet).ToString(),
                Selected = false
            });

            return walletItems;
        }
    }
}
